﻿package com.yy.ui.containers {
    import com.yy.ui.core.UIComponent;
    import com.yy.ui.events.UIEvent;
    
    import flash.display.*;
    import flash.events.*;

	/**
	 *选项卡组件 
	 * @author YY
	 * 
	 */
    public class TabNavigator extends UIComponent {

        public static const CENTER:String = "center";
        public static const LEFT:String = "left";
        public static const RIGHT:String = "right";

        private var _tabBarBaseContainer:Sprite;//选项卡容器
        private var _tabVector:Array;//现象卡项对象容器
        public var distance:uint = 5;//选项卡间的距离
        public var swapTabBarBase:Boolean;
        private var _index:int;
        private var _tabWidth:int;
        private var _selectedTabIndex:uint;
        private var _tabBarWidth:Number = 0;

        public function TabNavigator(){
            this._index = 0;
            this._tabBarBaseContainer = new Sprite();
            addChild(this._tabBarBaseContainer);
            this._tabVector = [];
        }
		/**
		 *添加选项卡 
		 * @param tb
		 * 
		 */
        public function addTab(tb:TabBarBase):void{
            if (tb){
				tb.selectedIndex = this._index;
				tb.addEventListener(TabBarBase.TAB_BAR_CLICK, this.tabBarClick);
                if (this._index != 0){
					tb.labelX = ((TabBarBase(this._tabVector[(this._index - 1)]).labelX + TabBarBase(this._tabVector[(this._index - 1)]).labelWidth) + this.distance);
                    this._tabWidth = (this._tabWidth + (TabBarBase(this._tabVector[(this._index - 1)]).labelWidth + this.distance));
                } else {
                    this._tabWidth = (this._tabWidth + tb.labelWidth);
                };
                this._index++;
                this._tabVector.push(tb);
                this._tabBarBaseContainer.addChildAt(tb, 0);
            };
        }
        public function resizeTabLabel(dir:String):void{
            var _local2:int;
            var _local3:int;
            var _local4:TabBarBase;
            switch (dir){
                case CENTER:
                    if (this._tabBarWidth != 0){
                        _local2 = ((this._tabBarWidth - this._tabWidth) / 2);
                    } else {
                        _local2 = ((TabBarBase(this._tabVector[this._selectedTabIndex]).width - this._tabWidth) / 2);
                    };
                    _local3 = 0;
                    while (_local3 < this._tabVector.length) {
                        _local4 = (this._tabVector[_local3] as TabBarBase);
                        _local4.labelX = (_local2 + ((_local4.labelWidth + this.distance) * _local3));
                        _local3++;
                    };
                    break;
                case LEFT:
                    break;
                case RIGHT:
                    break;
            };
        }
		/**
		 *模拟点击选项卡 
		 * @param i
		 * 
		 */
        public function set selectedTab(i:uint):void{
            this._selectedTabIndex = i;
            if (i < this._tabVector.length){
                TabBarBase(this._tabVector[i]).labelSelected = true;
                if (this.swapTabBarBase){
                    this._tabBarBaseContainer.setChildIndex(TabBarBase(this._tabVector[i]), 0);
                } else {
                    this._tabBarBaseContainer.setChildIndex(TabBarBase(this._tabVector[i]), (this._tabBarBaseContainer.numChildren - 1));
                };
                this.dispatchTabClickEvent();
            };
        }
		/**
		 *点击选项卡项处理 
		 * @param e
		 * 
		 */
        private function tabBarClick(e:Event):void{
            var tb:TabBarBase = (e.currentTarget as TabBarBase);
            var i:int;
            while (i < this._tabVector.length) {
                if (tb != this._tabVector[i]){
                    TabBarBase(this._tabVector[i]).labelSelected = false;
                } else {
                    this._selectedTabIndex = i;
                };
                i++;
            };
            if (this.swapTabBarBase){
                this._tabBarBaseContainer.setChildIndex(tb, 0);
            } else {
                this._tabBarBaseContainer.setChildIndex(tb, (this._tabBarBaseContainer.numChildren - 1));
            };
            tb.labelSelected = true;
            this.dispatchTabClickEvent();
        }
		/**
		 *发送点击事件 
		 * 
		 */
        private function dispatchTabClickEvent():void{
            var e:UIEvent = new UIEvent(UIEvent.TAB_CLICK);
            e.data = this._selectedTabIndex;
            dispatchEvent(e);
        }
        private function initView():void{
        }
        public function disposeView():void{
        }
		/**
		 *设置选项卡的宽度 
		 * @param value
		 * 
		 */
        public function set tabBarWidth(value:Number):void{
            this._tabBarWidth = value;
        }
		
		
		/**
		 *刷新组件 
		 * 
		 */
        override public function updateUI():void{
            super.updateUI();
        }

    }
}
